//
//  ProductListViewController.m
//  AmazingShopping
//
//  Created by Tan on 4/16/13.
//  Copyright (c) 2013 Tan Hoang. All rights reserved.
//

#import "ProductListViewController.h"
#import "SearchResultCell.h"

@interface ProductListViewController ()

@end

@implementation ProductListViewController

@synthesize categoryID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    serverPath = [(AppDelegate *)[[UIApplication sharedApplication] delegate] serverPath];
    [self getProductArray];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}

- (void)setCategoryID:(NSString *)cid{
    if (![cid isEqualToString:categoryID]) {
        categoryID = [cid copy];
    }
}

- (void)getProductArray{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/get_product_by_category", serverPath]];
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc]initWithURL:url];
    [request addPostValue:[NSString stringWithFormat:@"%@", categoryID] forKey:@"category_id"];
    int offset = 0;
    if (productArray.count > 0) {
        offset = productArray.count;
    }
    [request addPostValue:[NSString stringWithFormat:@"%d", offset] forKey:@"offset"];
    [request setDelegate:self];
    [request startAsynchronous];
}

#pragma mark ASIHTTPRequest Delegates
- (void)requestFailed:(ASIHTTPRequest *)request{
    
}

- (void)requestFinished:(ASIHTTPRequest *)request{
    NSData *data = [request responseData];
    JSONDecoder *jsonDecoder = [[JSONDecoder alloc]initWithParseOptions:JKParseOptionNone];
    productArray = [jsonDecoder objectWithData:data];
    [self.tableView reloadData];
}

#pragma mark Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}

- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Init cell
    static NSString *SearchResultIdentifier = @"SearchResultIdentifier";
    UINib *nib = [UINib nibWithNibName:@"SearchResultCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:SearchResultIdentifier];
    SearchResultCell *cell = [self.tableView dequeueReusableCellWithIdentifier:SearchResultIdentifier];
    
    int row = [indexPath row];
    //Set cell data
    [cell setProductName: [[productArray objectAtIndex:row] valueForKey:@"product_name"]];
    [cell setPrice: [[productArray objectAtIndex:row] valueForKey:@"product_price"]];
    [cell setSellerName:[[productArray objectAtIndex:row] valueForKey:@"seller_name"]];
    [cell setProductImagePath:[[[productArray objectAtIndex:row] valueForKey:@"product_images"] objectAtIndex:0]];
    [cell setReviewNumber: (NSInteger*)[[[productArray objectAtIndex:row] valueForKey:@"product_review_number"] integerValue]];
    [cell setProductRate: [[[productArray objectAtIndex:row] valueForKey:@"product_review"] floatValue]];
    
    //Set cell style
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark Table View Delegates

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

@end
